package com.wxzz.elearing.comment.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.comment.dto.SkillCommentDto;
import com.wxzz.elearing.comment.dto.SkillDto;
import com.wxzz.elearing.comment.dto.SkillPublishDto;
import com.wxzz.elearing.comment.dto.SkillVideoLogDto;
import com.wxzz.elearing.comment.entity.CommunityComment;
import com.wxzz.elearing.comment.entity.Skill;
import com.wxzz.elearing.comment.entity.SkillCenterCategory;
import com.wxzz.elearing.comment.entity.SkillComment;
import com.wxzz.elearing.comment.service.ISkillService;
import com.wxzz.elearing.comment.service.ISkillVideoLogService;
import com.wxzz.elearing.comment.vo.SkillPublishVo;
import com.wxzz.elearing.comment.vo.SkillVideoLogVo;
import com.wxzz.elearing.comment.vo.SkillVo;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.iterators.ObjectArrayListIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 技能 前端控制器(backend)
 * </p>
 *
 * @author 
 * @since 2021-05-17
 */
@RestController
@RequestMapping("/skill")
@Api(tags = "技能接口(企业)")
public class SkillController {

    @Autowired
    private ISkillService skillService;

    @Autowired
    private ISkillVideoLogService skillVideoLogService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "新增技能(企业)", notes = "新增或修改技能(企业)", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "saveSkill", method = RequestMethod.POST)
    public ReturnVO saveSkill(@RequestHeader String token, @Valid @RequestBody Skill skill) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long userId = jsonObject.getLong("id");
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        skill.setUserId(userId);
        skill.setEnterpriseId(enterpriseId);
        skill.setIsBackend(true); // 标记后台添加
        return skillService.saveSkill(skill);
    }

    // 废除
//    @ApiOperation(value = "审核技能通过(企业)", notes = "审核技能通过(企业)", httpMethod = "POST", tags = "企业")
//    @RequestMapping(value = "checkStatusSuccess/{id}", method = RequestMethod.POST)
//    public ReturnVO checkStatusSuccess(@PathVariable Long id) {
//        return skillService.checkSkill(id, 1, true);
//    }
//
//    @ApiOperation(value = "审核技能失败(企业)", notes = "审核技能失败(企业)", httpMethod = "POST", tags = "企业")
//    @RequestMapping(value = "checkStatusFail/{id}", method = RequestMethod.POST)
//    public ReturnVO checkStatusFail(@PathVariable Long id) {
//        return skillService.checkSkill(id, 2, true);
//    }

    @ApiOperation(value = "根据id获取详情(企业)", notes = "根据id获取详情(企业)", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "listDetail/{id}", method = RequestMethod.POST)
    public ReturnVO<SkillVo> listDetail(@RequestHeader String token, @PathVariable Long id) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        return skillService.selectSkillDetail(id, enterpriseId, null);
    }

    @ApiOperation(value = "查询所有技能(企业)", notes = "查询所有技能(企业)-分页", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillVo>>> list(@RequestHeader String token, @RequestBody SkillDto skillDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        return skillService.listSkill(skillDto, enterpriseId, null);
    }

    @ApiOperation(value = "根据技能id列出所有评论(企业)", notes = "根据技能id列出所有评论(企业)", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "listComment", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillComment>>> listComment(@RequestBody SkillCommentDto skillCommentDto) {
        return skillService.listComment( skillCommentDto );
    }

    @ApiOperation(value = "删除技能(企业)", notes = "删除技能(企业)", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    public ReturnVO delete(@RequestHeader String token, @PathVariable Long id) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        return skillService.deleteSkill(id, 0L, enterpriseId, true);
    }

    @ApiOperation(value = "查询发布排名(企业)", notes = "查询发布排名(企业)-分页", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "listRank", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillPublishVo>>> listRank(@RequestHeader String token, SkillPublishDto skillPublishDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        return skillService.listSkillPublishRank(skillPublishDto, enterpriseId);
    }

    @ApiOperation(value = "列出技能的观看列表(企业)", notes = "列出技能的观看列表(企业)-分页", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "listSkillWatcher/{skillId}", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillVideoLogVo>>> listSkillWatcher(@RequestHeader String token, @RequestBody SkillVideoLogDto skillVideoLogDto,  @PathVariable Long skillId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        return skillVideoLogService.listSkillWatchLog(skillVideoLogDto, skillId);
    }

    @ApiOperation(value = "查询所有技能(总后台)", notes = "查询所有技能(总后台)-分页", httpMethod = "POST", tags = "总后台")
    @RequestMapping(value = "listByPlatform", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillVo>>> listByPlatform(@RequestHeader String token, @RequestBody SkillDto skillDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = skillDto.getEnterpriseId();
        if (enterpriseId==null || enterpriseId==0){
            throw new ServiceException("请填写企业ID");
        }
        return skillService.listSkill(skillDto, enterpriseId, null);
    }


}
